package br.com.imob.util;


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import br.com.imob.modelo.AcompanhamentoFisico;
import br.com.imob.modelo.AcompanhamentoJuridico;
import br.com.imob.modelo.Agenda;
import br.com.imob.modelo.Bairro;
import br.com.imob.modelo.Categoria;
import br.com.imob.modelo.Cidade;
import br.com.imob.modelo.Classificacao;
import br.com.imob.modelo.Cliente;
import br.com.imob.modelo.Construtora;
import br.com.imob.modelo.Conta;
import br.com.imob.modelo.Contato;
import br.com.imob.modelo.Contrato;
import br.com.imob.modelo.Corretor;
import br.com.imob.modelo.Empresa;
import br.com.imob.modelo.Endereco;
import br.com.imob.modelo.Estado;
import br.com.imob.modelo.Face;
import br.com.imob.modelo.Finalidade;
import br.com.imob.modelo.Grupo;
import br.com.imob.modelo.Imovel;
import br.com.imob.modelo.Login;
import br.com.imob.modelo.Negocio;
import br.com.imob.modelo.Obra;
import br.com.imob.modelo.Origem;
import br.com.imob.modelo.Pais;
import br.com.imob.modelo.Pessoa;
import br.com.imob.modelo.Plano;
import br.com.imob.modelo.Relacionamento;
import br.com.imob.modelo.Situacao;
import br.com.imob.modelo.TipoImovel;


/**
 * Hibernate Utility class with a convenient method to get Session Factory object.
 *
 * @author Gilton Nascimento
 */
public class HibernateUtil {

    private static HibernateUtil hibb;
    private Session session;
    private static final SessionFactory sessionFactory;

    public static HibernateUtil getInstance() {
        if(hibb == null)
            hibb = new HibernateUtil();
        return hibb;
    }


    static {
        try {
            sessionFactory = new Configuration()
                .setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect")
                .setProperty("hibernate.connection.driver_class", "org.postgresql.Driver")
                .setProperty("hibernate.connection.url", "jdbc:postgresql://localhost:5432/imob")
                .setProperty("hibernate.connection.username", "postgres")
                .setProperty("hibernate.connection.password", "postgres")
                .setProperty("hibernate.show_sql", "true")
                .setProperty("hibernate.format_sql", "true")
                .setProperty("hibernate.default_schema", "public")
                    .setProperty("hibernate.c3p0.acquire_increment", "1")
                    .setProperty("hibernate.c3p0.idle_test_period", "100")
                    .setProperty("hibernate.c3p0.max_size", "10")
                    .setProperty("hibernate.c3p0.max_statements", "0")
                    .setProperty("hibernate.c3p0.min_size", "5")
                    .setProperty("hibernate.c3p0.timeout", "100")
                .addAnnotatedClass(AcompanhamentoJuridico.class)
                .addAnnotatedClass(AcompanhamentoFisico.class)
                .addAnnotatedClass(Agenda.class)
                .addAnnotatedClass(Bairro.class)
                .addAnnotatedClass(Categoria.class)
                .addAnnotatedClass(Cidade.class)
                .addAnnotatedClass(Classificacao.class)
                .addAnnotatedClass(Cliente.class)
                .addAnnotatedClass(Construtora.class)
                .addAnnotatedClass(Conta.class)
                .addAnnotatedClass(Contato.class)
                .addAnnotatedClass(Contrato.class)
                .addAnnotatedClass(Corretor.class)
                .addAnnotatedClass(Empresa.class)
                .addAnnotatedClass(Endereco.class)
                .addAnnotatedClass(Estado.class)
                .addAnnotatedClass(Face.class)
                .addAnnotatedClass(Finalidade.class)
                .addAnnotatedClass(Grupo.class)
                .addAnnotatedClass(Imovel.class)
                .addAnnotatedClass(Login.class)
                .addAnnotatedClass(Negocio.class)
                .addAnnotatedClass(Obra.class)
                .addAnnotatedClass(Origem.class)
                .addAnnotatedClass(Pais.class)
                .addAnnotatedClass(Pessoa.class)
                .addAnnotatedClass(Plano.class)
                .addAnnotatedClass(Relacionamento.class)
                .addAnnotatedClass(Situacao.class)
                .addAnnotatedClass(TipoImovel.class)
                .buildSessionFactory();
        } catch (Throwable ex) {
            System.err.println("A sessão inicial, SessionFactory, falhou." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public Session getSession() {
        session = getSessionFactory().openSession();
        session.beginTransaction();
        return session;
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

}